package com.uenpay.im.ui.old.activity.label;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.contacts.ChooseContactsActivity;
import com.uenpay.im.weight.MyDialog;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2015-12-28 11:43:08
 * @copyRight http://www.qiyunxin.com
 * @function 我的标签列表
 */
public class LabelListActivity extends BaseActivity {

    private View loading;
    private LabelListAdapter labelListAdapter;
    private ArrayList<LabelEntity> arrayList = new ArrayList<LabelEntity>();
    private ListView listView;
    private myBroadcastReceiver broadcastReceiver;

    private LabelHandle mLabelHandle = new LabelHandle();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_list_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        getLables();
    }

    protected void initView() {
        loading = findViewById(R.id.loading);
        listView = (ListView) findViewById(R.id.listview);
        ((TextView) findViewById(R.id.title_textview)).setText(getResources()
                .getString(R.string.all_label));
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources()
                .getString(R.string.add));
        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
    }

    protected void initListener() {
        //返回键点击
        findViewById(R.id.back_layout).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });

        findViewById(R.id.title_right_layout).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        Intent intent = new Intent(LabelListActivity.this,
                                ChooseContactsActivity.class);
                        intent.putExtra("isAddLabel", true);
                        startActivity(intent);
                    }
                });

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                LabelEntity labelEntity = (LabelEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (labelEntity != null) {
                    Intent intent = new Intent(LabelListActivity.this,
                            CreateLabelActivity.class);
                    intent.putExtra("labelMembers", labelEntity.arrayList);
                    intent.putExtra("labelName", labelEntity.labelName);
                    intent.putExtra("labelId", labelEntity.labelId);
                    startActivityForResult(intent, 101);
                }
            }
        });

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                                           int arg2, long arg3) {
                LabelEntity labelEntity = (LabelEntity) arg0.getAdapter()
                        .getItem(arg2);
                if (labelEntity != null) {
                    deleteLabelDailog(labelEntity.labelId);
                }
                return true;
            }
        });
    }

    private void getLables() {
        loading.setVisibility(View.VISIBLE);
        mLabelHandle.getMyLabels(new LabelHandle.IGetMyLabelsListener() {

            @Override
            public void onGetMyLabelsResult(int status, String msg,
                                            ArrayList<LabelEntity> _arrayList) {
                myHandler.sendEmptyMessage(1);
                if (status == HttpInvokeResultCode.RESULT_OK
                        && _arrayList != null && _arrayList.size() > 0) {
                    arrayList.clear();
                    arrayList = _arrayList;
                    labelListAdapter = new LabelListAdapter(
                            LabelListActivity.this, arrayList);
                    listView.setAdapter(labelListAdapter);
                } else {
                    if (arrayList != null) {
                        arrayList.clear();
                    }
                    if (labelListAdapter != null)
                        labelListAdapter.notifyDataSetChanged();
                }
            }
        });

    }

    private void deleteLabelDailog(final int label_id) {
        MyDialog.Builder alertDialog = new MyDialog.Builder(
                LabelListActivity.this);
        alertDialog.setTitle(R.string.dialog_title);
        alertDialog.setMessage(getResources().getString(
                R.string.delete_label_dialog));
        alertDialog.setPositiveButton(R.string.yes,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        deleteLables(label_id);
                    }
                });

        alertDialog.setNegativeButton(R.string.no,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        alertDialog.create().show();

    }

    private void deleteLables(final int label_id) {
        loading.setVisibility(View.VISIBLE);

        mLabelHandle.deleteLabel(label_id, new LabelHandle.ILabelResultListener() {

            @Override
            public void onLabelResult(int status, String msg) {
                myHandler.sendEmptyMessage(1);
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    for (int i = 0, size = arrayList.size(); i < size; i++) {
                        if (arrayList.get(i).labelId == label_id) {
                            arrayList.remove(i);
                            break;
                        }
                    }
                    labelListAdapter.notifyDataSetChanged();
                } else if (!TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });

    }

    private Handler myHandler = new Handler(new Handler.Callback() {

        @Override
        public boolean handleMessage(Message arg0) {
            if (arg0.what == 1) {
                if (loading != null) {
                    loading.setVisibility(View.GONE);
                }
            }

            return false;
        }
    });

    private class myBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            if (arg1.getAction().equals(BroadcastAction.LABEL_ADD_LABEL_ACTION)) {
                getLables();
            }
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == 101) {
            getLables();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onResume() {
        if (broadcastReceiver == null) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(BroadcastAction.LABEL_ADD_LABEL_ACTION);
            broadcastReceiver = new myBroadcastReceiver();
            registerReceiver(broadcastReceiver, intentFilter);
        }
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
